package order.service;

import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import common.Resquest;
import order.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import resgistor.Video;
import resgistor.VideoOrder;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class OrderServices {
    static{
        FlowRule rule = new FlowRule();
        List<FlowRule> rules=new ArrayList<>();
        rule.setGrade(1);
        rule.setResource("*");
        rule.setCount(2);
        rules.add(rule);
        com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager.loadRules(rules);
    }
    @Resource
    Resquest resquest;
    @Resource
    OrderDao orderDao;
    @LoadBalanced
    @Bean
    RestTemplate restTemplate(){
       return new RestTemplate();
    }
    @Autowired
    @LoadBalanced
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;
    @Bean
    Video video(){
       return new Video();
    }
    @Autowired
    Video video;
    @ResponseBody
    public Resquest addVideoOrder(Integer id, String videoTitle) {

            if(id!=null){
                System.out.println(LocalDateTime.now());

                System.out.println(restTemplate.getUriTemplateHandler().expand("http://xdclass-video-service/api/video/c/getVideoOrder2",Video.class).getPort());
                Integer port = discoveryClient.getInstances("xdclass-order-service").get(0).getPort();
                video.setSendInfo(port+"");
                System.out.println(video.getSendInfo());
//                orderDao.addOrder(videoTitle, LocalDateTime.now());
                return resquest.sueccess(200, "成功",video);

        }

            return resquest.error(401, "失败");


    }
    @ResponseBody
    public Resquest searchOrder(Integer id, String videoTitle) {
        if (id != null){
           List<VideoOrder> videoOrder= orderDao.searchOrder(id);

          Integer port = discoveryClient.getInstances("xdclass-order-service").get(0).getPort();
          String host= discoveryClient.getInstances("xdclass-order-service").get(0).getHost();
         Integer port3= restTemplate.getUriTemplateHandler().expand("http://"+host+":"+port+"/api/video/c/searchOrder",VideoOrder.class).getPort();
          String port2= restTemplate.getUriTemplateHandler().expand("http://xdclass-order-service/api/video/c/searchOrder",Video.class).getPort()+"";
          videoOrder.get(0).setResentInfo(port+"");
          return resquest.sueccess(200, "成功", videoOrder.get(0).getResentInfo());
        }
        return resquest.error(401, "失败");
    }
    @ResponseBody
    public Resquest addVideoOrder2(Integer id, String title) {
        if(Objects.nonNull( id)&& Objects.nonNull(title)){
            List<VideoOrder> videoOrder= orderDao.searchOrder2(title);
            return resquest.sueccess(200, "成功", videoOrder);
        }
        return resquest.error(401, "失败");
    }
}
